﻿<%@ Control Language="C#" AutoEventWireup="true" Inherits="Pages_Operators_Ascx_ProgramPopup"
    CodeBehind="ProgramPopup.ascx.cs" %>
<div id="program-dialog" title="Default Program">
    <table cellspacing="10" class="popup">
        <tr>
            <td>
                <div class="item">
                    <div class="label">
                        Day:</div>
                    <div class="field">
                        <asp:HiddenField ID="hidRouteServiceID" runat="server" />
                        <asp:DropDownList ID="ddlDayOfRouteInput" runat="server" Width="68px">
                        </asp:DropDownList>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"
                            ControlToValidate="ddlDayOfRouteInput"></asp:RequiredFieldValidator>
                        of every route
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="item">
                    <div class="label">
                        Service #:</div>
                    <div class="field">
                        <asp:HiddenField ID="hidServiceID" runat="server" />
                        <asp:DropDownList ID="ddlServiceCodeInput" runat="server">
                            <asp:ListItem Value="-1" class="firstItem">Select a service</asp:ListItem>
                        </asp:DropDownList>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
                            ControlToValidate="ddlServiceCodeInput"></asp:RequiredFieldValidator>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="item">
                    <div class="label">
                        Service Name:</div>
                    <div class="field">
                        <asp:TextBox ID="txtServiceNameInput" runat="server" Enabled="false"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                            ControlToValidate="txtServiceNameInput"></asp:RequiredFieldValidator>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="item">
                    <div class="label">
                        Activity:</div>
                    <div class="field">
                        <asp:TextBox ID="txtProgramInput" runat="server"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="*"
                            ControlToValidate="txtProgramInput"></asp:RequiredFieldValidator>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="item">
                    <div class="label">
                        Hotel pickup:</div>
                    <div class="field">
                        <asp:TextBox ID="txtHotelPickupAddressInput" runat="server"></asp:TextBox></div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="item">
                    <div class="label">
                        Type:</div>
                    <div class="field">
                        <asp:DropDownList ID="ddlServiceTypeInput" runat="server">
                        </asp:DropDownList>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="*"
                            ControlToValidate="ddlServiceTypeInput"></asp:RequiredFieldValidator>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="item">
                    <span id="spanNoticePopup">
                        <asp:Literal ID="litNoticePopup" runat="server" />
                    </span>
                </div>
            </td>
        </tr>
    </table>
</div>
<script type="text/javascript">
    $("#<%=ddlServiceCodeInput.ClientID %>").change(function () {
        $("#<%=hidServiceID.ClientID %>").val($("#<%=ddlServiceCodeInput.ClientID %> option:selected").val());
        $("#<%=txtServiceNameInput.ClientID %>").val($("#<%=ddlServiceCodeInput.ClientID %> option:selected").attr('ServiceName'));
    });

    $("#<%=hidServiceID.ClientID %>").val($("#<%=ddlServiceCodeInput.ClientID %> option:selected").val());
    $("#<%=txtServiceNameInput.ClientID %>").val($("#<%=ddlServiceCodeInput.ClientID %> option:selected").attr('ServiceName'));

    function clearProgramForm() {
        $("#<%=hidRouteServiceID.ClientID %>").removeAttr('index');
        $("#<%=hidRouteServiceID.ClientID %>").val('');
        $("#<%=ddlDayOfRouteInput.ClientID %>").val(-1)
        $("#<%=txtProgramInput.ClientID %>").val('');
        $("#<%=txtHotelPickupAddressInput.ClientID %>").val('');

        $("#<%=ddlDayOfRouteInput.ClientID %>").removeAttr('disabled');

        $("#<%=ddlDayOfRouteInput.ClientID %>").focus();

    }

    function insertProgram() {
        var result = false;

        var tbody = $('#gridProgram > tbody');
        var evenClass = '';
        var buttons = '<a routeserviceid="' + $("#<%=hidRouteServiceID.ClientID %>").val() + '" class="btnEdit new">Edit</a>';
        buttons += '|&nbsp;<a routeserviceid="' + $("#<%=hidRouteServiceID.ClientID %>").val() + '" class="btnDelete new">Inactive</a>';
        var serviceCode = '';
        if ($("#<%=hidServiceID.ClientID %>").val() != "-1")
            serviceCode = $("#<%=ddlServiceCodeInput.ClientID %> option:selected").text();

        try {
            var row = stringformat('<tr{0}><td><input type="checkbox" class="chkService" /></td>' +
                            '<td style="display: none"></td>' +
                            '<td style="display: none">{1}</td>' +
                            '<td align="center">{2}</td>' +
                            '<td>{3}</td>' +
                            '<td>{4}</td>' +
                            '<td>{5}</td>' +
                            '<td>{6}</td>' +
                            '<td>{7}</td>' +
                            '<td style="display: none">{8}</td>' +
                            '<td style="display: none">{9}</td>' +
                            '<td>{10}</td></tr>',
                            evenClass,
                            $("#<%=ddlDayOfRouteInput.ClientID %> option:selected").val(), // DayOfRoute ID
                            $("#<%=ddlDayOfRouteInput.ClientID %> option:selected").text(), // DayOfRoute Display
                            $("#<%=txtProgramInput.ClientID %>").val(), // Activity
                            $("#<%=txtHotelPickupAddressInput.ClientID %>").val(),
                            serviceCode, // ServiceCode
                            $("#<%=txtServiceNameInput.ClientID %>").val(), // ServiceName
                            $("#<%=ddlServiceTypeInput.ClientID %> option:selected").text(),
                            $("#<%=hidServiceID.ClientID %>").val(), // ServiceID
                            $("#<%=ddlServiceTypeInput.ClientID %> option:selected").val(),
                            buttons);
            if ($.trim(tbody.html()) == '') {
                tbody.html(row);
            }
            else {
                var idx = $("#<%=ddlDayOfRouteInput.ClientID %>").val();
                var lastTR;
                $('tr', tbody).each(function () {
                    if (parseInt($(this).children("td:eq(2)").text()) <= idx)
                        lastTR = $(this);
                });
                lastTR.after(row);
                // $('tr:last', tbody).after(row);
            }

            setEvents();

            $("#<%=ddlDayOfRouteInput.ClientID %>").focus();

            $("#litNoticePopup").text("INSERT SUCCESSFUL.");

            result = true;
        }
        catch (e) {
            $("#litNoticePopup").text("INSERT FAILS!");
        }
        return result;
    }

    function updateProgram() {
        var result = false;
        try {

            var line = $("#gridProgram > tbody > tr:eq(" + $("#<%=hidRouteServiceID.ClientID %>").attr('index') + ")");
            $(line).children("td:eq(4)").text($("#<%=txtProgramInput.ClientID %>").val());
            $(line).children("td:eq(5)").text($("#<%=txtHotelPickupAddressInput.ClientID %>").val());
            $(line).children("td:eq(6)").text($("#<%=ddlServiceCodeInput.ClientID %> option:selected").text());
            $(line).children("td:eq(7)").text($("#<%=txtServiceNameInput.ClientID %>").val());
            $(line).children("td:eq(8)").text($("#<%=ddlServiceTypeInput.ClientID %> option:selected").text());
            $(line).children("td:eq(9)").text($("#<%=hidServiceID.ClientID %>").val());
            $(line).children("td:eq(10)").text($("#<%=ddlServiceTypeInput.ClientID %> option:selected").val());

            $("#litNoticePopup").text("UDPATE SUCCESSFUL.");

            result = true;
        }
        catch (e) {
            $("#litNoticePopup").text("UPDATE FAILS!");
        }

        return result;
    }

    $("#program-dialog").dialog({
        autoOpen: false,
        width: 680,
        modal: true,
        buttons: {
            "OK": function () {
                var isNew = $("#<%=hidRouteServiceID.ClientID %>").attr('index') == undefined;
                if (isNew) {
                    if (insertProgram()) clearProgramForm();
                }
                else {
                    if (updateProgram()) $(this).dialog("close");
                }
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
        }
    });

    function editProgram(idx) {
        var line = $("#gridProgram > tbody > tr:eq(" + idx + ")");
        $("#<%=hidRouteServiceID.ClientID %>").attr('index', idx);
        $("#<%=hidRouteServiceID.ClientID %>").val($(line).children("td:eq(1)").text());
        $("#<%=ddlDayOfRouteInput.ClientID %>").val(parseInt($(line).children("td:eq(2)").text()));
        $("#<%=txtProgramInput.ClientID %>").val($.trim($(line).children("td:eq(4)").text()));
        $("#<%=txtHotelPickupAddressInput.ClientID %>").val($.trim($(line).children("td:eq(5)").text()));
        $("#<%=ddlServiceCodeInput.ClientID %>").val($(line).children("td:eq(9)").text());
        $("#<%=ddlServiceTypeInput.ClientID %>").val(parseInt($(line).children("td:eq(10)").text()));

        $("#<%=txtServiceNameInput.ClientID %>").val($.trim($(line).children("td:eq(7)").text()));
        $("#<%=hidServiceID.ClientID %>").val($(line).children("td:eq(9)").text());
        $("#<%=ddlDayOfRouteInput.ClientID %>").attr('disabled', true);
    }


    function initializeEvents() {
        $("#gridProgram a.btnEdit.new").click(function () {
            var index = $("#gridProgram > tbody > tr").index($(this).closest('tr'));
            editProgram(index);

            $("#program-dialog").dialog("open");
        });

        $("#gridProgram a.btnEdit").removeClass('new');

        $("#gridProgram a.btnDelete.new").click(function () {

            var result = confirm("Are you sure you want to delete this record?");
            if (result) {
                var aTag = $(this);

                $('#gridProgram > tbody > tr').removeClass('selected');
                aTag.parents("tr:first").css('display', 'none');
                aTag.parents("tr:first").children("td:eq(11)").text("deleted");
            }
        });

        $("#gridProgram a.btnDelete").removeClass('new');
    }

    function getProgram(route) {
        route.RouteServiceCollection = [];
        $("table#gridProgram tbody tr").each(function () {
            if ($(this).children("td:eq(11)").text() != 'deleted') {
                var program = {};
                if ($(this).children("td:eq(1)").text() == '')
                    program.IsNew = true;
                else
                    program.RouteServiceID = $(this).children("td:eq(1)").text();

                program.DayOfRoute = parseInt($(this).children("td:eq(2)").text());
                program.Program = $(this).children("td:eq(4)").text();
                program.HotelPickupAddress = $(this).children("td:eq(5)").text();
                program.ServiceType = parseInt($(this).children("td:eq(10)").text());

                var serviceID = $.trim($(this).children("td:eq(9)").text());
                if (serviceID != null && serviceID != -1) // Select a Service
                    program.ServiceID = parseInt($(this).children("td:eq(9)").text());
                
                route.RouteServiceCollection.push(program);
            }
        });
    }


    function moveUp() {
        $("table#gridProgram tbody tr td input:checked").each(function () {
            var idx = $("table#gridProgram > tbody > tr").index($(this).closest('tr'));
            if (idx > 0)
                $('table#gridProgram tbody').moveRow(idx, idx - 1);
        });

    }

    function moveDown() {
        var length = $("table#gridProgram tbody tr").length - 1;
        $($("table#gridProgram tbody tr td input:checked").get().reverse()).each(function () {
            var idx = $("table#gridProgram > tbody > tr").index($(this).closest('tr'));
            if (idx < length)
                $('table#gridProgram tbody').moveRow(idx + 1, idx);
        });
    }
</script>
